package com.m3839.sdk.login.core;

import android.content.Context;
import android.content.SharedPreferences;

import com.m3839.sdk.common.core.SdkConfiguration;
import com.m3839.sdk.common.encrypt.Base64Utils;
import com.m3839.sdk.login.bean.InternalUser;

/**
 * @author ChenBaoyang
 * @description: HykbUserStorage
 * @date 2021/11/16 11:02
 */
public class UserStorage {


    /**
     * 该文件同步付费的同一个文件名（为了在接入付费下载和内购SDK，登录授权只要一次）
     */
    private static final String FILE_NAME = "hykb_login_info";

    private static final String KEY_VALIDITY = "validity";

    private static final String KEY_FIRST_TIME = "timestamp";

    private static final String KEY_USER_STATE = "state";

    private static final String KEY_USER_NICK = "nick";

    private static final String KEY_FORM_FCM = "from_fcm";

    private static final String KEY_ORIENTATION = "orientation";

    /**
     * 这个字段文件，表示是否来自付费下载
     */
    private static final String KEY_FROM_PAID = "from_paid";


    private static String getFileName() {
        return FILE_NAME + SdkConfiguration.getGameId();
    }

    /**
     * 保存用户的uid|token|type 数据串密码串
     *
     * @param context
     * @param value  {@link com.m3839.sdk.login.utils.UserUtils#getState(InternalUser)}
     * @return
     */
    public static boolean saveUserState(Context context, String value) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(KEY_USER_STATE, value);
        return editor.commit();
    }

    /**
     * 保存用户的uid|token|type 数据串密码串
     *
     * @param context
     * @return
     */
    public static String getUserState(Context context) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        String value = settings.getString(KEY_USER_STATE, "");
        return Base64Utils.decode(value);
    }

    /**
     * 保存用户的昵称
     *
     * @param context
     * @param value  {@link InternalUser#getNick()}
     * @return
     */
    public static boolean saveUserNick(Context context, String value) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(KEY_USER_NICK, value);
        return editor.commit();
    }

    /**
     * 获取用户的昵称
     *
     * @param context
     * @return
     */
    public static String getUserNick(Context context) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        String value = settings.getString(KEY_USER_NICK, "");
        return value;
    }


    /**
     * 保存用户有效的天数
     *
     * @param context
     * @param value
     * @return
     */
    public static boolean saveValidity(Context context, int value) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(KEY_VALIDITY, Base64Utils.encode(String.valueOf(value)));
        return editor.commit();
    }


    /**
     * 保存截止日期
     *
     * @param context
     * @param value
     * @return
     */
    public static boolean saveFirstTime(Context context, long value) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(KEY_FIRST_TIME, value);
        return editor.commit();
    }

    /**
     * 获取截止日期
     *
     * @param context
     * @return
     */
    public static long getFirstTime(Context context) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        long value = settings.getLong(KEY_FIRST_TIME, 0L);
        return value;
    }

    /**
     * 获取用户信息来自付费下载
     *
     * @param context
     * @return
     */
    public static boolean getFlag(Context context) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        boolean value = settings.getBoolean(KEY_FROM_PAID, false);
        return value;
    }

    /**
     * 保存用户信息来自防沉迷sdk的标志
     *
     * @param context
     * @param value
     * @return
     */
    public static boolean saveFromFcm(Context context, boolean value) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(KEY_FORM_FCM, value);
        return editor.commit();
    }

    /**
     * 获取用户信息来自防沉迷sdk的标志
     *
     * @param context
     * @return
     */
    public static boolean getFromFcm(Context context) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        boolean value = settings.getBoolean(KEY_FORM_FCM, false);
        return value;
    }

    /**
     * 保存用户有效的天数
     *
     * @param context
     * @param value
     * @return
     */
    public static boolean saveOrientation(Context context, int value) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(KEY_ORIENTATION, value);
        return editor.commit();
    }

    /**
     * 获取用户有效的天数
     *
     * @param context
     * @return
     */
    public static int getOrientation(Context context) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        int value = settings.getInt(KEY_ORIENTATION,0);
        return value;
    }

    /**
     * 保存用户的实名认证状态
     *
     * @param context
     * @param uid 用户UID
     * @param value 该用户的认证状态
     * @return
     */
    public static boolean saveRealNameStatus(Context context, String uid, int value) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(uid, value);
        return editor.commit();
    }

    /**
     * 获取用户的实名认证状态
     *
     * @param context
     * @param uid 用户UID
     * @return 该用户的认证状态
     */
    public static int getRealNameStatus(Context context, String uid) {
        SharedPreferences settings = context.getSharedPreferences(getFileName(), Context.MODE_PRIVATE);
        int value = settings.getInt(uid,0);
        return value;
    }

    /**
     * 清除掉本地缓存
     * @param context
     */
    public static void clearCache(Context context) {
        saveFirstTime(context, 0L);
        saveUserState(context, "");
        saveUserNick(context, "");
    }
}
